import AntModal from '@/components/pt/dialog/AntModal'
import { getTinaBill, addTinaBill, updateTinaBill } from '@/api/tina/bill'
import { listTinaSupplier } from '@/api/tina/supplier'
import { listTinaMaterial } from '@/api/tina/material'

export default {
  name: 'CreateForm',
  props: {},
  components: {
    AntModal
  },
  data() {
    return {
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      disabled: false,
      total: 0,
      id: undefined,
      formTitle: '添加采购订单',
      // 表单参数
      form: {},
      supplierList: [],
      materialList: [],
      rules: {},
      columns: [
        {
          title: '原材料',
          dataIndex: 'materialId',
          width: '30%',
          scopedSlots: { customRender: 'materialId' }
        },
        {
          title: '价格',
          dataIndex: 'price',
          scopedSlots: { customRender: 'price' }
        },
        {
          title: '数量',
          dataIndex: 'count',
          scopedSlots: { customRender: 'count' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ]
    }
  },
  filters: {},
  created() {
    listTinaSupplier().then(response => {
      this.supplierList = response.data.list
    })
    listTinaMaterial().then(response => {
      this.materialList = response.data.list
    })
  },
  computed: {},
  watch: {},
  mounted() {
  },
  methods: {
    addItem() {
      const item = {
        materialId: '',
        price: 0,
        count: 0
      }
      this.form.billItems.push(item)
    },
    removeItem(item) {
      const index = this.form.billItems.indexOf(item)
      if (index !== -1) {
        this.form.billItems.splice(index, 1)
      }
    },
    onClose() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        supplierId: undefined,
        shipPrice: undefined,
        discountPrice: undefined,
        billItems: [],
        remark: undefined
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.formTitle = '添加采购订单'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.open = true
      this.spinning = !this.spinning
      const billId = row.id
      getTinaBill(billId).then(response => {
        this.form = response.data
        this.formTitle = '修改采购订单'
        this.spinning = !this.spinning
      })
    },
    /** 提交按钮 */
    submitForm: function(closeDialog) {
      this.closeDialog = closeDialog
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form))
          if (this.form.id !== undefined) {
            updateTinaBill(saveForm).then(response => {
              this.$message.success('更新成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          } else {
            addTinaBill(saveForm).then(response => {
              this.$message.success('新增成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back() {
      const index = '/tina/bill/index'
      this.$router.push(index)
    }

  }
}
